package com.cmwa.scbp.business.customerEquity.prdFeeYield.controller;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cmwa.scbp.business.customerEquity.prdFeeYield.entity.FeeYieldVo;
import com.cmwa.scbp.business.customerEquity.prdFeeYield.entity.PrdFeeYieldVo;
import com.cmwa.scbp.business.customerEquity.prdFeeYield.service.PrdFeeYieldExportService;
import com.cmwa.scbp.business.customerEquity.prdFeeYield.service.PrdFeeYieldService;
import com.cmwa.scbp.business.product.entity.TreFeeDetailVo;
import com.cmwa.scbp.business.product.entity.TreFeeRateChangeVo;
import com.cmwa.scbp.business.product.entity.TreFeeTypeVo;
import com.cmwa.scbp.base.mvc.controller.BaseController;
import com.cmwa.scbp.base.mvc.entity.Page;
import com.cmwa.scbp.base.mvc.entity.Result;
import com.cmwa.scbp.base.mvc.entity.SearchParam;
import com.cmwa.scbp.base.exception.ValidateFailedException;
import com.cmwa.scbp.utils.JsonUtil;
import com.cmwa.scbp.utils.SessionUtils;
import com.cmwa.scbp.utils.TimeUtil;

import net.sf.json.JSONArray;

/**
 * 产品费用与收益
 * 
 * @author ex-zhangys
 * @date 2019-01-22 10:30:12
 */
@Controller
@RequestMapping("/business/customerEquity/prdFeeYield")
public class PrdFeeYieldController extends BaseController{
	
	private final static String PAGE_PACKAGE_PATH_NEW = "business/customerEquity/prdFeeYield/";
	
	@Resource
	private PrdFeeYieldService prdFeeYieldService;
	
	@Resource
	private PrdFeeYieldExportService prdFeeYieldExportService;
	
	@RequestMapping("/prdFeeYieldList")	
    public String prdFeeYieldList(){
      return PAGE_PACKAGE_PATH_NEW + "prdFeeYieldList";
	}
	
	@RequestMapping("/prdFeeYieldListForCustomerEquity")	
    public String prdFeeYieldListForCustomerEquity(@RequestParam("feeType")String feeType,
    		@RequestParam("dateType")String dateType,
    		@RequestParam("type")String type,ModelMap model){
	    String time=TimeUtil.getTime(type);
		String[] times=time.split(",");
		String str = "";
		str += "{";
		str += "'beginDate':'" + times[0] + "',";
		str += "'endDate':'" + times[1] + "',";
		str += "'feeType':'" + feeType+ "',";
		str += "'dateType':'" + dateType + "'";
		str +="}";
	  model.addAttribute("parameter", str);
      return PAGE_PACKAGE_PATH_NEW + "prdFeeYieldList";
	}
	
	/**
	 * 一层列表查询
	 * @param sp
	 * @return
	 */
	@RequestMapping("/prdFeeYieldListPage")
	@ResponseBody
	public Page<PrdFeeYieldVo> prdFeeYieldListPage(SearchParam sp){
		//获取当前操作人
		//sp.getSp().put("currUserId", SessionUtils.getEmployee().getID());
		return prdFeeYieldService.prdFeeYieldListPage(sp);
	}
	
	/**
	 * 新增
	 * @param subPrdCode
	 * @param subFundsName
	 * @param model
	 * @return
	 */
	@RequestMapping("/addPrdFeeOrYieldView")
	public String addPrdFeeOrYieldView(@RequestParam("subPrdCode") String subPrdCode,
			@RequestParam("subFundsName") String subFundsName,ModelMap model){
		model.put("subPrdCode", subPrdCode);
		model.put("subFundsName", subFundsName);
		return PAGE_PACKAGE_PATH_NEW + "prdFeeOrYieldAdd";
	}
	
	/**
	 * 新增保存
	 * @param model
	 * @return
	 */
	@RequestMapping("/prdFeeOrYieldAdd")
	public String prdFeeOrYieldAdd(FeeYieldVo vo,ModelMap model) {
		prdFeeYieldService.prdFeeOrYieldAdd(vo);
		return redirectSuccess();
	}
	
	/**
	 * 修改
	 * @param subPrdCode
	 * @param subFundsName
	 * @param model
	 * @return
	 */
	@RequestMapping("/updateFeeOrYieldInfoView")
	public String updateFeeOrYieldInfoView(@RequestParam("feeId") String feeId,
			@RequestParam("cashFlowsDate") String cashFlowsDate,
			@RequestParam("feeName") String feeName,
			@RequestParam("oldFeeName") String oldFeeName,
			@RequestParam("subFundsName") String subFundsName,
			@RequestParam("type") String type,
			ModelMap model){
		model.put("feeId", feeId);
		model.put("cashFlowsDate", cashFlowsDate);
		model.put("feeName", feeName);
		model.put("oldFeeName", oldFeeName);
		model.put("subFundsName", subFundsName);
		model.put("type", type);
		
		SearchParam sp = new SearchParam();
		sp.getSp().put("feeId", feeId);
		sp.getSp().put("cashFlowsDate", cashFlowsDate);
		sp.getSp().put("type", type);
		FeeYieldVo vo=prdFeeYieldService.feeYield(sp);
		model.put("vo", vo);
		
		return PAGE_PACKAGE_PATH_NEW + "feeOrYieldInfoUpdate";
	}
	
	/**
	 * 修改保存
	 * @param model
	 * @return
	 */
	@RequestMapping("/feeOrYieldInfoUpdate")
	public String feeOrYieldInfoUpdate(FeeYieldVo vo,ModelMap model) {
		vo.setModifyId(SessionUtils.getEmployee().getID());
		prdFeeYieldService.feeOrYieldInfoUpdate(vo);
		return redirectSuccess();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/feeOrYieldInfoDelete")
	@ResponseBody
	public String feeOrYieldInfoDelete(@RequestParam("feeId") String feeId,
			@RequestParam("cashFlowsDate") String cashFlowsDate,
			@RequestParam("type") String type){
		SearchParam sp = new SearchParam();
		sp.getSp().put("feeId", feeId);
		sp.getSp().put("cashFlowsDate", cashFlowsDate);
		sp.getSp().put("type", type);
		prdFeeYieldService.feeOrYieldInfoDelete(sp);
		return "success";
	}
	
	/**
	 * 验证还本日期
	 */
	@RequestMapping("/vReturnPlan")
	@ResponseBody
	public String vReturnPlan(@RequestParam("subPrdCode")String subPrdCode,
			@RequestParam("cashFlowsDate")String cashFlowsDate){
		SearchParam sp = new SearchParam();
		sp.getSp().put("subPrdCode", subPrdCode);
		sp.getSp().put("cashFlowsDate", cashFlowsDate);
		String result=prdFeeYieldService.vReturnPlan(sp);
		return result;
	}
	
	/**
	 * 导出
	 * @param sp
	 * @return
	 */
	@RequestMapping("/prdFeeYieldExportTo")
	public void prdFeeYieldExportTo(SearchParam sp, HttpServletResponse response) throws IOException{
		//获取当前操作人
		//sp.getSp().put("currUserId", SessionUtils.getEmployee().getID());
		prdFeeYieldExportService.export(response, sp);
	}
	
	/**
	 * 总览页面导出
	 * @param sp
	 * @return
	 */
	@RequestMapping("/exportToExcel")
	public void exportToExcel(SearchParam sp
			, HttpServletResponse response
			,@RequestParam("type")String type) throws IOException{
		String time=TimeUtil.getTime(type);
		String[] times=time.split(",");
		sp.getSp().put("expectPayDateBegin", times[0]);
		sp.getSp().put("expectPayDateEnd", times[1]);
//		sp.getSp().put("bnsDateBegin", times[0]);
//		sp.getSp().put("bnsDateEnd", times[1]);
		//获取当前操作人
		//sp.getSp().put("currUserId", SessionUtils.getEmployee().getID());
		prdFeeYieldExportService.export(response, sp);
	}
	
	/**
	 * 获取预计付款金额和实际付款金额
	 */
	@RequestMapping("/getYJandSJAmount")
	@ResponseBody
	public FeeYieldVo getYJandSJAmount(@RequestParam("type")String type){
		String time=TimeUtil.getTime(type);
		String[] times=time.split(",");
		SearchParam sp = new SearchParam();
		sp.getSp().put("expectPayDateBegin", times[0]);
		sp.getSp().put("expectPayDateEnd", times[1]);
		sp.getSp().put("bnsDateBegin", times[0]);
		sp.getSp().put("bnsDateEnd", times[1]);
		FeeYieldVo vo=prdFeeYieldService.queryYJandSJAmount(sp);
		return vo;
	}
	
	/**
	 * 获取预计份额数量和实际份额数量
	 */
	@RequestMapping("/getYJandSJCount")
	@ResponseBody
	public FeeYieldVo getYJandSJCount(@RequestParam("type")String type){
		String time=TimeUtil.getTime(type);
		String[] times=time.split(",");
		SearchParam sp = new SearchParam();
		sp.getSp().put("expectPayDateBegin", times[0]);
		sp.getSp().put("expectPayDateEnd", times[1]);
		sp.getSp().put("bnsDateBegin", times[0]);
		sp.getSp().put("bnsDateEnd", times[1]);
		FeeYieldVo vo=prdFeeYieldService.queryYJandSJCount(sp);
		return vo;
	}
	
	/**
	 * 二层列表查询
	 * @param sp
	 * @return
	 */
	@RequestMapping("/feeYieldListPage")
	@ResponseBody
	public Page<FeeYieldVo> feeYieldListPage(SearchParam sp){
		//获取当前操作人
		//sp.getSp().put("currUserId", SessionUtils.getEmployee().getID());
		return prdFeeYieldService.feeYieldListPage(sp);
	}
	
	@RequestMapping("/queryFeeYieldBySubPrdCodeListPage")
	@ResponseBody
	public Page<FeeYieldVo> queryFeeYieldBySubPrdCodeListPage(SearchParam sp) {
		return prdFeeYieldService.queryFeeYieldBySubPrdCodeListPage(sp);
	}
	
	@RequestMapping("/queryChangFeeYieldByFeeIdListPage")
	@ResponseBody
	public Page<FeeYieldVo> queryChangFeeYieldByFeeIdListPage(SearchParam sp) {
		return prdFeeYieldService.queryChangFeeYieldByFeeIdListPage(sp);
	}
	
	/**
	 * test
	 * @return
	 */
	@RequestMapping("/feeYieldList")
	public String feeYieldList() {
		return "business/productManager/feeYieldList";
	}
	
	/**
	* @throws Exception 
	*/
	@RequestMapping("/savePrdFeeDetail")
	public String savePrdFeeDetail(TreFeeDetailVo treFeeDetailVo, 
			@RequestParam(value = "feeChangeListJson", required = false) String feeChangeListJson,
			ModelMap model) throws ValidateFailedException {
		//前台传递过来的对象不能为空
		if (null != treFeeDetailVo) {
			//将费率类型存入数据库中
			prdFeeYieldService.insertPrdFeeDetail(treFeeDetailVo);
			String feeRateType = treFeeDetailVo.getFeeRateType();
			//保存浮动费率信息
			if(StringUtils.isNotBlank(feeRateType) && "1".equals(feeRateType)) {
				String feeId = treFeeDetailVo.getFeeId();
				List<TreFeeRateChangeVo> feeRateChangeList = JsonUtil.parseListObject(feeChangeListJson, TreFeeRateChangeVo.class);
				if (CollectionUtils.isNotEmpty(feeRateChangeList)) {
					for (TreFeeRateChangeVo changeVo : feeRateChangeList) {
						changeVo.setFeeRate(changeVo.getChangeFeeRate());
						changeVo.setFeeId(feeId);
						prdFeeYieldService.insertPrdFeeRateChange(changeVo);
					}
				}
			}
			return redirectSuccess();
		}else {
			model.addAttribute("exception", "新增失败，参数不能为空");
			return redirectExecFaild();
		}
	}
	
	@RequestMapping("/deletePrdFeeDetail")
	@ResponseBody
	public Result<?> deletePrdFeeDetail(@RequestParam("ids") String ids) {
		prdFeeYieldService.deletePrdFeeDetail(ids);
		Result<?> result = new Result<>();
		result.setSuccess(true);
		return result;
	}
	
	@RequestMapping("/feeTypeList")
	@ResponseBody
	public String feeTypeList() {
		List<TreFeeTypeVo> treFeeTypeList = prdFeeYieldService.feeTypeList();
		return JSONArray.fromObject(treFeeTypeList).toString();
	}
}
